package com.example.demo.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/demo1")
public class Test_1_15 {

    /**
     * 返回静态页面*/
    @RequestMapping("/m1")
    public String m1() {
        return "/index.html";
    }

    /**
     * 返回数据*/
    @ResponseBody
    @RequestMapping("/m2")
    public String m2() {
        return "m2中的字符串，非常爽！";
    }

    /**
     * 返回json数据
     * -- 无需进行特别的设置，当返回值需要包装为json时，会自动进行设置*/
    @ResponseBody
    @RequestMapping("/m3")
    public Map<String, String> m3() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "careathers");
        map.put("age", "18");
        map.put("address", "HuNan Province");
        map.put("school", "ChangShaSocialWorkCollege");
        return map;
    }

    /**
     * 设置状态码*/
    @ResponseBody
    @RequestMapping("/m4")
    public String m4(HttpServletResponse response) {
        // 设置状态码
        response.setStatus(500);
        return "m4，返回一个字符串！";
    }

    /**
     * 设置header
     * 1,通过@RequestMapping注解 指定返回的内容类型*/
    @ResponseBody
    @RequestMapping(value = "/m5", produces = "application/json;charset=utf-8;")
    public String m5() {
        return "伤过的心就像玻璃碎片";
    }

    /**
     * 2,通过@HttpServletResponse内置对象设置响应内容类型*/
    @ResponseBody
    @RequestMapping(value = "/m6")
    public String m6(HttpServletResponse response) {
        // 在响应头中新增字段
        response.setHeader("userName", "Lucy");
        return "爱情的蠢永远不会复现";
    }

}







